package ai.mspbots.test.controller;

import ai.mspbots.test.dto.ApiKeyDTO;
import ai.mspbots.test.entity.ApiKeyEntity;
import ai.mspbots.test.service.ApiKeyManagementService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API密钥管理控制器
 * 
 * @author mspbots
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/keys")
@RequiredArgsConstructor
public class ApiKeyController {

    private final ApiKeyManagementService apiKeyManagementService;

    /**
     * 获取所有API密钥
     */
    @GetMapping
    public ResponseEntity<List<ApiKeyEntity>> getAllApiKeys() {
        List<ApiKeyEntity> apiKeys = apiKeyManagementService.loadApiKey();
        return ResponseEntity.ok(apiKeys);
    }

    /**
     * 验证API密钥
     */
    @PostMapping("/validate")
    public ResponseEntity<Void> validateApiKeys() {
        apiKeyManagementService.validateApiKey();
        return ResponseEntity.ok().build();
    }

    /**
     * 保存API密钥
     */
    @PostMapping
    public ResponseEntity<Void> saveApiKey(@RequestBody ApiKeyDTO apiKeyDTO) {
        try {
            apiKeyManagementService.saveApiKey(apiKeyDTO);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("保存API密钥失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
}
